home *** CD-ROM | disk | FTP | other *** search
- //--------------------------------------------------------------------------
- //
- // Copyright (c) 2002, Colin Granville
- //
- // All rights reserved.
- //
- // Redistribution and use in source and binary forms, with or
- // without modification, are permitted provided that the following
- // conditions are met:
- //
- // * Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- //
- // * Redistributions in binary form must reproduce the above
- // copyright notice, this list of conditions and the following
- // disclaimer in the documentation and/or other materials
- // provided with the distribution.
- //
- // * The name Colin Granville may not be used to endorse or promote
- // products derived from this software without specific prior
- // written permission.
- //
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- // OF THE POSSIBILITY OF SUCH DAMAGE.
- //
- //--------------------------------------------------------------------------
-
- #include "Save.h"
- #include "DocView.h"
- #include "fstream.h"
- #include "strstream.h"
- #include "Document.h"
- #include "file.h"
- #include "DrawOutputDevice.h"
- #include "GuiHourGlass.h"
- #include "TextOutputdev.h"
- #include "PSOutputDev.h"
- #include "Page.h"
- #include "guilib:gfx.h"
-
- Save::Save(DocView& v, GuiWindow& ancestor)
- : saveAs("SaveAs"),
- view(v),
- aboutToBeShownTarget(&ancestor,GuiSaveAs::AboutToBeShown::Event,this,Save::aboutToBeShown),
- saveToFileTarget(&ancestor,GuiSaveAs::SaveToFile::Event,this,Save::saveAsDrawfile)
- {
- }
-
- //*************************************************************************
-
- Save::~Save() {}
-
- //*************************************************************************
-
- Claim Save::aboutToBeShown(GuiToolboxEvent&, const GuiIdBlock& id_block)
- {
- switch (id_block.parent.component)
- {
- case 0: //pdf
- saveAs.setFileType(0xadf);
- saveToFileTarget.setMethod(this,Save::saveAsPdf);
- break;
- case 2: //postscript
- saveAs.setFileType(0xff5);
- saveToFileTarget.setMethod(this,Save::saveAsPostscript);
- break;
- case 3: //text;
- saveAs.setFileType(0xfff);
- saveToFileTarget.setMethod(this,Save::saveAsText);
- break;
- case 4://whole doc as text
- saveAs.setFileType(0xfff);
- saveToFileTarget.setMethod(this,Save::saveAllAsText);
- break;
- case 5://whole doc as drawfile
- saveAs.setFileType(0x1000);
- saveToFileTarget.setMethod(this,Save::saveAllAsDrawfiles);
- break;
- case 6://page as postscript
- saveAs.setFileType(0xff5);
- saveToFileTarget.setMethod(this,Save::savePageAsPostscript);
- break;
- default: //drawfile
- saveAs.setFileType(0xaff);
- saveToFileTarget.setMethod(this,Save::saveAsDrawfile);
- }
- return CLAIM;
- }
-
- //*************************************************************************
-
- Claim Save::saveAsPdf(GuiToolboxEvent& ev,const GuiIdBlock&)
- {
- GuiSaveAs::SaveToFile& file = (GuiSaveAs::SaveToFile&)ev;
- ifstream in(view.getDocument().getFileName().c_str());
- ofstream out(file.filename);
- out << in.rdbuf();
- if ((in.good() && out.good())) setFileType(file.filename,0xadf);
- saveAs.fileSaveCompleted((in.good() && out.good()?1:0),file.filename);
- return CLAIM;
- }
-
- //*************************************************************************
-
- Claim Save::saveAsDrawfile(GuiToolboxEvent& ev,const GuiIdBlock&)
- {
- GuiSaveAs::SaveToFile& file = (GuiSaveAs::SaveToFile&)ev;
- int ok= (view.getOutDev().getDrawFile().save(file.filename)==0?1:0);
- saveAs.fileSaveCompleted(ok,file.filename);
- return CLAIM;
- }
-
- //*************************************************************************
-
-
- Claim Save::saveAsPostscript(GuiToolboxEvent& ev,const GuiIdBlock&)
- {
- GuiSaveAs::SaveToFile& file = (GuiSaveAs::SaveToFile&)ev;
-
- GuiHourglass hourglass;
- PSOutputDev out(
- file.filename,
- view.getDocument().getPDFDoc().getXRef(),
- view.getDocument().getCatalog(),
- 1,
- view.getDocument().getPageCount(),
- psModePS
- );
-
- bool ok=out.isOk();
- if (ok)
- {
- int i;
- int pages=view.getDocument().getPageCount();
- for (i=1;i<=pages;i++)
- {
- GuiHourglass::percentage(i*100/pages);
- view.getDocument().getPage(i,&out,72,0);
- if (gfx::inkey(-113)==-1) break;
- }
- }
- if (ok) setFileType(file.filename,0xff5);
- saveAs.fileSaveCompleted(ok?1:0,file.filename);
- return CLAIM;
- }
-
- //*************************************************************************
-
-
- Claim Save::savePageAsPostscript(GuiToolboxEvent& ev,const GuiIdBlock&)
- {
- GuiSaveAs::SaveToFile& file = (GuiSaveAs::SaveToFile&)ev;
-
- GuiHourglass hourglass;
- PSOutputDev out(
- file.filename,
- view.getDocument().getPDFDoc().getXRef(),
- view.getDocument().getCatalog(),
- view.getPage(),
- view.getPage(),
- psModePS
- );
-
- bool ok=out.isOk();
- if (ok)
- {
- view.getDocument().getPage(view.getPage(),&out,72,0);
- setFileType(file.filename,0xff5);
- }
- saveAs.fileSaveCompleted(ok?1:0,file.filename);
- return CLAIM;
- }
-
- //*************************************************************************
-
-
- Claim Save::saveAsText(GuiToolboxEvent& ev,const GuiIdBlock&)
- {
- GuiSaveAs::SaveToFile& file = (GuiSaveAs::SaveToFile&)ev;
- TextOutputDev out(file.filename,0,0,0);
- bool ok=out.isOk();
- GuiHourglass hourglass;
- if (ok)
- {
- view.getDocument().getPage(view.getPage(),&out,72,0);
- setFileType(file.filename,0xfff);
- }
- if (ok)
- saveAs.fileSaveCompleted((ok?1:0),file.filename);
- return CLAIM;
- }
-
- //*************************************************************************
-
- Claim Save::saveAllAsText(GuiToolboxEvent& ev,const GuiIdBlock&)
- {
- GuiSaveAs::SaveToFile& file = (GuiSaveAs::SaveToFile&)ev;
-
- TextOutputDev out(file.filename,0,0,0);
- bool ok=out.isOk();
- GuiHourglass hourglass;
- if (ok)
- {
- int i;
- int pages=view.getDocument().getPageCount();
- for (i=1;i<=pages;i++)
- {
- GuiHourglass::percentage(i*100/pages);
- view.getDocument().getPage(i,&out,72,0);
- if (gfx::inkey(-113)==-1) break;
- }
- setFileType(file.filename,0xfff);
- }
-
- saveAs.fileSaveCompleted((ok?1:0),file.filename);
- return CLAIM;
- }
-
- //*************************************************************************
-
- Claim Save::saveAllAsDrawfiles(GuiToolboxEvent& ev,const GuiIdBlock&)
- {
- GuiSaveAs::SaveToFile& file = (GuiSaveAs::SaveToFile&)ev;
-
- char buf[280];
- ostrstream name(buf,sizeof(buf));
-
- bool ok = (isDirectory(file.filename) || createDirectory(file.filename));
-
- DrawOutputDevice out;
- GuiHourglass hourglass;
- out.setMoreColours(gTrue);
- out.setNoImages(view.getChoices().getNoImages());
- out.setNoText(view.getChoices().getNoText());
-
- if (ok)
- {
- int dir=0;
- bool pathOk;
- int i;
- int pages=view.getDocument().getPageCount();
- for (i=1;i<=pages;i++)
- {
- pathOk=0;
- while (!pathOk)
- {
- name.seekp(0);
- name << file.filename << ".Pages";
- name.fill('0');
- name.width(2);
- name << dir << ".p";
- name.fill('0');
- name.width(4);
- name << i;
-
- ofstream check_file(name.str());
- if (!check_file)
- {
- dir++;
- name.seekp(0);
- name << file.filename<< ".Pages";
- name.fill('0');
- name.width(2);
- name << dir;
- if (!(isDirectory(name.str()) || createDirectory(name.str()) )) break;
- }
- else pathOk=1;
- }
-
- if (!pathOk) break;
-
- GuiHourglass::percentage(i*100/pages);
- view.getDocument().getPage(i,&out,DrawOutputDevice::DPI,0);
- if (out.getDrawFile().save(name.str())!=0) break;
- if (gfx::inkey(-113)==-1) break;
- }
- }
-
- saveAs.fileSaveCompleted((ok?1:0),file.filename);
- return CLAIM;
- }
-